﻿using System;
using System.Windows.Forms;
using System.Resources;
using EnvDTE;

namespace VsTortoiseSVN.OptionPages
{
    /// <summary>
    /// Represents the "General" options page in Main Menu -> Tools -> Options -> VsTortoise
    /// </summary>
    public partial class GeneralPage : PageBase
    {
        public GeneralPage()
        {
            InitializeComponent();
            InitializePage();
        }

        #region Webupdate Options
        private void ReadUpdateOptions()
        {
            switch(Options.Instance.Webupdate.Behaviour)
            {
                case WebupdateBehaviour.None:
                checkForUpdatesNeverRadio.Checked = true;
                    break;

                case WebupdateBehaviour.Startup:
                checkForUpdatesStartupRadio.Checked = true;
                    break;

                case WebupdateBehaviour.Interval:
                checkForUpdatesIntervalRadio.Checked = true;
                    break;
            }

            checkForUpdatesInterval.Value = Options.Instance.Webupdate.IntervalInDays;
            checkForUpdatesBetaToggle.Checked = Options.Instance.Webupdate.ConsiderBeta;
        }

        private void WriteUpdateOptions()
        {
            if (checkForUpdatesNeverRadio.Checked)
                Options.Instance.Webupdate.Behaviour = WebupdateBehaviour.None;

            if(checkForUpdatesStartupRadio.Checked)
                Options.Instance.Webupdate.Behaviour = WebupdateBehaviour.Startup;

            if (checkForUpdatesIntervalRadio.Checked)
                Options.Instance.Webupdate.Behaviour = WebupdateBehaviour.Interval;

            Options.Instance.Webupdate.IntervalInDays = System.Convert.ToInt32(checkForUpdatesInterval.Value);
            Options.Instance.Webupdate.ConsiderBeta = checkForUpdatesBetaToggle.Checked;
        }
        #endregion

        #region Diagnostics Options
        /// <summary>
        /// Diagnostics option settings are only valid for one session. Once
        /// Visual Studio restarts, settings must be done again
        /// </summary>
        void ReadDiagnosticsOptions()
        {
            diagnosticsToogle.Checked = Options.Instance.Diagnostics.Enabled;
        }

        void WriteDiagnosticsOptions()
        {
            Options.Instance.Diagnostics.Enabled = diagnosticsToogle.Checked;
        }
        #endregion

        #region IDTToolsOptionsPage Implementation
        public override void OnAfterCreated(DTE DTEObject)
        {
            System.Diagnostics.Trace.WriteLine("OptionPageFolders.OnAfterCreated");
            ReadUpdateOptions();
            ReadDiagnosticsOptions();
        }

        public override void OnOK()
        {
            System.Diagnostics.Trace.WriteLine("OptionPageFolders.OnOK");
            WriteUpdateOptions();
            WriteDiagnosticsOptions();

            Options.Instance.Apply();
            Options.Instance.Save();
        }
        #endregion
    }
}
